package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.Utils.DateUtils;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MemberServiceImpl
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-29
 * @Version V1.0
 **/
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport(){
        //获取最近一年的月份
        List<String> months = DateUtils.getThisYearMonths();
        //每月存储人数
        List<Long> memberCount = new ArrayList<>();
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("months", months);
        resultMap.put("memberCount", memberCount);
        return resultMap;
    }
}
